var nameRegex = /^[a-zA-ZÁÉÍÓÚáéíóuñÑ]+(([\'\,\.\- ][a-zA-ZÁÉÍÓÚáéíóuñÑ ])?[a-zA-ZÁÉÍÓÚáéíóuñÑ]*)*$/;
var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
var floatRegex = /^[0-9]*\.?[0-9]*$/;
var latlongRegex = /^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}/;
var webRegex = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/;
//http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html
var phoneRegex = /^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$/;
//5551212 | 614555-1212 | (614)555-1212
var dateRegex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/; // regular expression to match required date format
var num_regex = /^\d+$/; // numeric digits only
var username_regex = /^[\w.-]+$/;  // allowed characters: any word . -, ( \w ) represents any word
var password_regex = /^[A-Za-z\d]{6,8}$/;  // any upper/lowercase characters and digits, between 6 to 8 characters in total
var question_regex = /\?$/; // ends with a question mark

//Uso de regex
//http://eisabainyo.net/weblog/2009/04/30/10-examples-of-basic-input-validation-in-javascript/

var __formatDate = "dd/MM/yyyy";//getLocaleShortDateString();

var minYear = 1000;
var maxYear = 2100;

var errorMsg = "";

var msgDelay = 4;

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {    
  var msg;
  var msgcontent;
  if(!document.getElementById('_validation_msg')) {
    msg = document.createElement('div');
    msg.id = '_validation_msg';
    msgcontent = document.createElement('div');
    msgcontent.id = '_validation_msg_content';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('_validation_msg');
    msgcontent = document.getElementById('_validation_msg_content');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);

  window.location.hash = "#"+targetdiv.id;
  targetdiv.focus();
   
//  targetdiv.select();
//  window.location.hash(targetdiv.id);

  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('_validation_msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('_validation_msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "/images/msg_arrow.gif";
}

function _parseDate(_date){
    return getDateFromFormat(_date,__formatDate);
//  if (_date != '') {
//    if (regs = _date.match(dateRegex)) {
//        if( (regs[1] >= 1 || regs[1] <= 31) &&
//            (regs[2] >= 1 || regs[2] <= 12) &&
//            (regs[3] >= minYear || regs[3] <= maxYear) ) {
//
//            _dd = regs[1];
//            _mm = regs[2];
//            _yy = regs[3];
//
//            return new Date(_yy,_mm-1,_dd);
//        }
//    }
//  }
//  return false
}

  // Original JavaScript code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.  
  function checkDate(_date){
    errorMsg = ""
//    var _dd;var _mm;var _yy;
//
//    if(_date != '') {
//      if(regs = _date.match(dateRegex)) {
//        if(regs[1] < 1 || regs[1] > 31) {
//          errorMsg = "Valor inválido para día: " + regs[1];
//        } else if(regs[2] < 1 || regs[2] > 12) {
//          errorMsg = "Valor inválido para mes: " + regs[2];
//        } else if(regs[3] < minYear || regs[3] > maxYear) {
//          errorMsg = "Valor inválido para año: " + regs[3] + ". Debería estar entre " + minYear + " y " + maxYear;
//        }
//      } else {
//        errorMsg = "Formato de fecha inválido: " + _date;
//      }
//    } else {
//      errorMsg = "Debe ingresar una fecha.";
//    }
//
//    return errorMsg == "";
    if (!isDate(_date,__formatDate)){
        errorMsg = "Debe ingresar una fecha válida.";
        return false;
    }
    else{
        errorMsg = "";
        return true;
    }  
  }